﻿package pluginfw.nio.message
{
    import flash.utils.*;
    import pluginfw.*;
    import pluginfw.nio.encrypt.*;

    public class SimpleMessage extends Object implements ISimpleMessage
    {
        protected var buffer:ByteArray;
        protected var iscompressed:Boolean = false;
        protected var size:int;
        protected var messageid:int;
        protected var crc:int;
        protected var type:int;
        protected var group:int;
        protected var cmd:int;
        protected var p:Object;

        public function SimpleMessage(param1:Boolean = false)
        {
            this.buffer = new ByteArray();
            this.iscompressed = param1;
            return;
        }// end function

        function prase(param1:int, param2:int, param3:int, param4:ByteArray) : void
        {
            this.size = param1;
            this.messageid = param2;
            this.crc = param3;
            this.buffer.position = 0;
            this.buffer.writeInt(param1);
            this.buffer.writeByte(0);
            this.buffer.writeShort(param2);
            this.buffer.writeShort(param3);
            param4.readBytes(this.buffer, this.buffer.position, param1 - 5);
            return;
        }// end function

        public function getSize() : int
        {
            return this.size;
        }// end function

        public function getMessageId() : int
        {
            return this.messageid;
        }// end function

        public function getCrc() : int
        {
            return this.crc;
        }// end function

        public function getType() : int
        {
            return this.type;
        }// end function

        public function getBuffer() : ByteArray
        {
            this.buffer.position = 0;
            return this.buffer;
        }// end function

        public function getGroup() : int
        {
            if (this.group == 0)
            {
                this.buffer.position = 9;
                this.group = this.buffer.readUnsignedByte();
            }
            return this.group;
        }// end function

        public function getCmd() : int
        {
            if (this.cmd == 0)
            {
                this.buffer.position = 10;
                this.cmd = this.buffer.readUnsignedShort();
            }
            return this.cmd;
        }// end function

        public function getParam()
        {
            var bytes:ByteArray;
            if (this.p == null)
            {
                this.buffer.position = 12;
                if (this.buffer.bytesAvailable > 0)
                {
                    bytes = new ByteArray();
                    this.buffer.readBytes(bytes, 0);
                    CRC16.decode(bytes, this.getCrc());
                    bytes.position = 0;
                    if (this.iscompressed)
                    {
                        bytes.uncompress();
                    }
                    try
                    {
                        this.p = bytes.readObject();
                    }
                    catch (error:Error)
                    {
                        p = new Object();
                    }
                }
            }
            return this.p;
        }// end function

        public function toString() : String
        {
            return StringUtil.substitute("SimpleMessage:{5},messageid={3},size:{4},group={0},cmd={1},params={2}", this.getGroup(), this.getCmd(), this.getParam(), this.getMessageId(), this.getSize(), this.iscompressed);
        }// end function

    }
}
